package com.example.controller;

import cn.hutool.core.util.StrUtil;
import com.example.dto.LoginDTO;
import com.example.dto.RegisterDTO;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.WebService;
import com.example.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/web")
@Slf4j
public class WebController {

    @Autowired
    private WebService webService;

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录，参数：{}", loginDTO);
        if (StrUtil.isBlank(loginDTO.getUsername()) || StrUtil.isBlank(loginDTO.getPassword())) {
            return Result.error(500, "数据输入不合法");
        }
        //登录校验
        UserVO userVO = webService.login(loginDTO);
        return Result.success(userVO);
    }

    /**
     * 注册
     *
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册，参数：{}", registerDTO);
        if (StrUtil.isBlank(registerDTO.getUsername()) || StrUtil.isBlank(registerDTO.getPassword())) {
            return Result.error(500, "数据输入不合法");
        }
        webService.register(registerDTO);
        return Result.success();
    }

    /**
     * 获取登录的用户信息
     *
     * @return
     */
    @GetMapping
    public Result getUser() {
        log.info("获取登录用户信息...");
        User user = webService.getUser();
        return Result.success(user);
    }
}
